package com.yanqu.road.server.manger;

import com.yanqu.road.dao.impl.servercenter.HotPackVersionDaoImpl;
import com.yanqu.road.entity.servercenter.ChannelHotUpdateConfigVersion;
import com.yanqu.road.server.TempMgr;

import java.util.List;
import java.util.Map;
import java.util.Objects;

public class ChannelHotUpdateConfigMgr extends TempMgr {

    private static Map<String, List<ChannelHotUpdateConfigVersion>> channelHotUpdateConfigMap;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        channelHotUpdateConfigMap = new HotPackVersionDaoImpl().getChannelHotUpdateConfigVersionList();
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static ChannelHotUpdateConfigVersion getChannelHotUpdateConfigVersion(String packageMark, String hotfixVersion, int scope){
        if(!channelHotUpdateConfigMap.containsKey(packageMark)){
            return null;
        }
        List<ChannelHotUpdateConfigVersion> list = channelHotUpdateConfigMap.get(packageMark);
        for (ChannelHotUpdateConfigVersion configVersion : list) {
            if(Objects.equals(hotfixVersion, configVersion.getHotfixVersion()) && configVersion.getUpdateScope().contains(scope+"")){
                return configVersion;
            }
        }
        return null;
    }
}
